<template>
  <div class="main-layout">
    <main>
      <slot />
    </main>
    <footer>
      <div
        @click="$router.push(it.path)"
        v-for="it in footerList"
        :key="it.icon"
        :class="['item', it.active ? 'active' : '']"
      >
        <i :class="[it.icon]"></i>
        <span>{{ it.text }}</span>
      </div>
    </footer>
  </div>
</template>

<script>
export default {
  data() {
    return {
      footerList: [
        { icon: "home", text: "首页", active: false, path: "/home" },
        { icon: "videos", text: "视频", active: false, path: "/videos" },
        { icon: "message", text: "消息", active: false, path: "/message" },
        { icon: "mine", text: "我的", active: false, path: "/mine" },
      ],
    };
  },
  // 每个页面都会重新加载这个组件，直接监听路由地址，根据路由地址选中active项
  watch: {
    "$route.path": {
      immediate: true,
      handler(_path) {
        this.footerList = this.footerList.map((it) => {
          it.active = it.path === _path;
          return it;
        });
      },
    },
  },
};
</script>

<style lang="less" scoped>
    @import "../style/common.less";

    // 定义一个循环的方法
    @icons: home videos message mine;
    .loop(@index) when (@index <= 4){
        @icon: extract(@icons, @index);

        > i{
            &.@{icon}{
                 background-image: url('../images/footer/@{icon}.png');
            }
        }
        &.active{
            >i{
                &.@{icon}{
                    background-image: url('../images/footer/@{icon}-active.png');
                }
            }
        }
        .loop(@index + 1)
    }
    
    .main-layout{
        display: block;
        height: 100%;

        > main {
            height: calc(100% - 49px);
            // 当设置横向滚动为关闭的时候，不设置纵向，那么纵向就是自动滚动的
            overflow-x: hidden;
            background: #e9ecf1;
        }
        > footer{
            height: 49px;
            border-top: solid 2px #eeeeee;
            background: #fff;
            border-radius: 20px;
            .item{
                float: left;
                width: 25%;
            
                > i {
                    display: block;
                    height: 30px;
                    background-position: center center;
                    background-size: 22px;
                    background-repeat: no-repeat;
               
                }
     
                > span {
                    display: block;
                    height: 16px;
                    text-align: center;
                    font-size: .6rem;
                    color: #545456;
                }
                .loop(1);

                &.active {
                    > span {
                        color: #ff9c00;
                    }
                }
            }
        }

    }
</style>

